<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/30
 * Time: 17:15
 */

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
class StoresProfitRecord extends Model
{
    /**
     * 门店返币收益表
     * @var string
     */
    protected $table = 'stores_profit_record';
    protected $fillable = [
        'id',
        'store_number',
        'currency',
        'source',
        'source_store',
        'profit_type',
        'consum_type',
        'ordersn',
        'addtime',
        'updated_at',
        'created_at' 
    ];

        /**
     * 添加记录
     * @param array $data
     * @return bool
     */
    public function add($data = array()) {
        if (empty($data)) {
            return false;
        }
        $rico = $this->create($data);
        if ($rico) {
            return $rico;
        } else {
            return false;
        }
    }



    /**
     * 获取全部收益激励
     * @param array $keyword
     * @param bool $pag
     * @return bool|\Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function get_record($keyword = array(),$pag =true) {
        $table = $this;
        //时间查询
        if( !empty($keyword['addtime']) ){
            $table =$this->where('addtime','>',strtotime($keyword['addtime'][0]))->where('addtime','<',strtotime($keyword['addtime'][1]));
        }

        //消费类型
        if(!empty($keyword['consum_type'])){
            $table = $this->where(['consum_type' => $keyword['consum_type']]);
        }
      
        //门店收益类型
        if(isset($keyword['profit_type'])){
            $table = $this->where(['profit_type' => $keyword['profit_type']]);
        }
        //关键字
        if(!empty($keyword['keywords'])){
            $table = $this->where('store_number','like','%'.$keyword['keywords'].'%')->orWhere('ordersn','like','%'.$keyword['keywords'].'%');
        }
        if( $pag ){
            $table = $table->select('id', 'store_number', 'currency', 'source', 'source_store', 'addtime', 'profit_type', 'consum_type', 'ordersn' )->orderBy('addtime','desc');
            $total = $table->sum('currency');
            $rico['total'] = $total;
            $rico['record'] = $table->paginate(15);
        } else {
            $rico = $table->select('id', 'store_number', 'currency', 'source', 'source_store', 'addtime', 'profit_type', 'consum_type', 'ordersn' )->orderBy('addtime','desc')->get()->toArray();
        }
        if ($rico) {
            return $rico;
        } else {
            return false;
        }
    }


}